<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="流程状态" prop="processid">
        <el-select
          v-model="queryParams.processid"
          placeholder="请选择流程状态"
          clearable
          @keyup.enter.native="handleQuery"
          style="width: 220px;"
        >
          <el-option :label="'待处理流程'" :value="1" />
          <el-option :label="'已处理流程'" :value="0" />
        </el-select>
      </el-form-item>
      <el-form-item label="购买类别" prop="oprtype">
        <el-select
          v-model="queryParams.oprtype"
          placeholder="请选择购买类别"
          clearable
          @keyup.enter.native="handleQuery"
          style="width: 220px;"
        >
          <el-option :label="'买房'" :value="'买房'" />
          <el-option :label="'换房'" :value="'换房'" />
          <el-option :label="'退房'" :value="'退房'" />
        </el-select>
      </el-form-item>
      <el-form-item label="是否有效" prop="invalid">
        <el-select
          v-model="queryParams.invalid"
          placeholder="请选择是否有效"
          clearable
          @keyup.enter.native="handleQuery"
          style="width: 220px;"
        >
          <el-option :label="'有效'" :value="'有效'" />
          <el-option :label="'作废'" :value="'作废'" />
        </el-select>
      </el-form-item>
      <el-form-item label="房产名称" prop="housename">
        <el-input
          v-model="queryParams.housename"
          placeholder="请输入房产名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户名称" prop="custname">
        <el-input
          v-model="queryParams.custname"
          placeholder="请输入客户名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="BuyHouseList" size="mini">
      <el-table-column label="编号" align="center" prop="buyid" />
      <el-table-column label="客户名称" align="center" prop="custname" />
      <el-table-column label="房产名称" align="center" prop="housename" width="150px"/>
      <el-table-column label="进程序号" align="center" prop="processid" />
      <el-table-column label="进程名称" align="center" prop="processname" />
      <el-table-column label="付款方案" align="center" prop="psid" />
      <el-table-column label="成交单价" align="center" prop="unitprice" />
      <el-table-column label="装修单价" align="center" prop="fitmentunitprice" />
      <el-table-column label="签约单价" align="center" prop="signunitprice" />
      <el-table-column label="购房总款" align="center" prop="totalmoney" />
      <el-table-column label="装修总款" align="center" prop="fitmenttotalprice" />
      <el-table-column label="签约总款" align="center" prop="signtotalprice" />
      <el-table-column label="按揭银行" align="center" prop="bankid"  width="120px"/>
      <el-table-column label="首期款" align="center" prop="firstmoney" />
      <el-table-column label="按揭金额" align="center" prop="bankmoney" />
      <el-table-column label="装修首款" align="center" prop="fitmentfirstprice" />
      <el-table-column label="装修尾款" align="center" prop="fitmentlastprice" />
      <el-table-column label="状态" align="center" prop="oprtype" />
      <el-table-column label="订/退时间" align="center" prop="buytime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.buytime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="原定房产" align="center" prop="housename2"  width="150px"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="140px">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            v-if="scope.row.oprtype != '退房'"
            @click="handleProcess(scope.row)"
          >处理流程</el-button>
          <el-button
            size="mini"
            type="text"
            @click="checkout(scope.row)"
            v-if="scope.row.oprtype != '退房'"
            v-hasPermi="['house_sale:BuyHouse:checkout']"
          >退房</el-button>
          <el-button
            size="mini"
            type="text"
            v-if="scope.row.oprtype != '退房'"
            @click="houseChange(scope.row)"
            v-hasPermi="['house_sale:BuyHouse:change']"
          >换房</el-button>
          <el-button
            size="mini"
            type="text"
            v-if="scope.row.oprtype == '退房'"
            onclick=""
          >已退房</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 购房流程对话框 -->
    <el-dialog :title="'购房流程'" :visible.sync="open" width="89%" append-to-body>
      <WorkProcessStep :hourseid="form.hourseid" :refresh="processRefresh"/>
    </el-dialog>

    <!-- 退房对话框 -->
    <el-dialog title="退房" :visible.sync="checkoutOpen" width="500px" append-to-body>
      <Checkout :buyhouse="form" :refresh="checkoutRefresh" @next="finish"/>
    </el-dialog>

    <!-- 换房对话框 -->
    <el-dialog title="换房" :visible.sync="changeOpen" width="89%" append-to-body>
      <ChooseNewHouse :mode="'change'" @next="buyinfo"/>
    </el-dialog>

    <!-- 换房购买对话框 -->
    <el-dialog title="完善数据" :visible.sync="buyOpen" width="89%" append-to-body>
      <BuyHouse :buyid="form.buyid" :custid="form.custid" :houseinfo="houseinfo" :mode="'change'" @goback="chooseHouse" @next="handleChange"/>
    </el-dialog>
  </div>
</template>

<script>
import WorkProcessStep from '@/views/house_sale/components/workprocess/step';
import Checkout from '@/views/house_sale/components/houseevent/checkout';
import ChooseNewHouse from '@/views/house_sale/salesGuide/index';
import BuyHouse from '@/views/house_sale/components/BuyHouse/index';
import { listBuyHouse, delBuyHouse } from "@/api/house_sale/BuyHouse";

export default {
  name: "WorkProcess",
  components: {
    WorkProcessStep,
    Checkout,
    ChooseNewHouse,
    BuyHouse
  },
  data() {
    return {
      // 购房流程更新变量
      processRefresh: 0,
      // 退房更新变量
      checkoutRefresh: 0,
      // 退房对话框
      checkoutOpen: false,
      // 换房对话框
      changeOpen: false,
      // 换房流程
      buyOpen: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 购买房产表格数据
      BuyHouseList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        processid: null,
        custname: null,
        housename: null,
        invalid: null,
        oprtype: null,
      },
      // 表单参数
      form: {},
      // 更换房产信息
      houseinfo: {}
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询购买房产列表 */
    getList() {
      this.loading = true;
      listBuyHouse(this.queryParams).then(response => {
        this.BuyHouseList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 处理流程
    handleProcess(val) {
      if(val.oprtype != '退房') {
        this.form = val;
        this.processRefresh = ++this.processRefresh;
        this.open = true;
      } else {
        this.$modal.msgWarning("该房已退");
      }
    },
    // 退房
    checkout(val) {
      if(val.oprtype != '退房') {
        this.form = val;
        this.checkoutRefresh = ++this.checkoutRefresh;
        this.checkoutOpen = true;
      } else {
        this.$modal.msgWarning("该房已退");
      }
    },
    // 选择房间
    chooseHouse() {
      this.buyOpen = false;
      this.changeOpen = true;
    },
    // 换房
    houseChange(val) {
      this.form = val;
      this.changeOpen = true;
    },
    buyinfo(houseinfo) {
      this.changeOpen = false;
      this.houseinfo = houseinfo;
      this.buyOpen = true;
    },
    // 换房操作
    handleChange() {
      this.getList();
      this.buyOpen = false;
    },
    // 退房完成
    finish() {
      this.checkoutOpen = false;
      this.getList();
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          // if (this.form.buyid != null) {
          //   updateBuyHouse(this.form).then(response => {
          //     this.$modal.msgSuccess("修改成功");
          //     this.open = false;
          //     this.getList();
          //   });
          // } else {
          //   addBuyHouse(this.form).then(response => {
          //     this.$modal.msgSuccess("新增成功");
          //     this.open = false;
          //     this.getList();
          //   });
          // }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const buyids = row.buyid || this.ids;
      this.$modal.confirm('是否确认删除购买房产编号为"' + buyids + '"的数据项？').then(function() {
        return delBuyHouse(buyids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('house_sale/BuyHouse/export', {
        ...this.queryParams
      }, `BuyHouse_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
